<template>
    <div id="macarons" :class="className" :style="{height:height,width:width}"/>
</template>

<script>
import echarts from 'echarts'

require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'
import { getChartData } from '@/api/monitor/visits'
import { findResAndConNum } from '@/api/app/appInf'

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      chart: null,
      sidebarElm: null,
      paramData: { timeType: '1', nowDate: '' },
      taskData: {
        legend: [],
        times: [],
        seriesList: [
          // {
          //    name: 'AURORA810-并发数',
          //    type: 'bar',
          //    data: [2, 4, 7, 9, 25, 76, 13, 16, 32, 20, 6, 3]
          //  },
          //
          //  {
          //    name: 'AURORA810-资源数',
          //    type: 'line',
          //    yAxisIndex: 1,
          //    data: [90, 120, 130, 45, 63, 102, 203, 234, 230, 165, 120, 62]
          //  },
          //  {
          //    name: 'BLUEMOON-并发数',
          //    type: 'bar',
          //    data: [6, 9, 9, 26, 28, 70, 175, 182, 48, 18, 6, 2]
          //  },
          //  {
          //    name: 'BLUEMOON-资源数',
          //    type: 'line',
          //    yAxisIndex: 1,
          //    data: [20, 22, 35, 45, 63, 102, 103, 134, 130, 165, 89, 162]
          //  }
        ]
      }
    }
  },
  created() {
    this.paramData.nowDate = this.$moment(new Date()).format('YYYY-MM-DD HH:mm:ss')
  },
  mounted() {
    findResAndConNum(this.paramData).then(res => {
      res.seriesList.forEach(element => {
        element.symbol = 'none'
      })
      this.taskData = res
      this.initChart()
    })
    if (this.autoResize) {
      this.__resizeHandler = debounce(() => {
        if (this.chart) {
          this.chart.resize()
        }
      }, 100)
      window.addEventListener('resize', this.__resizeHandler)
    }

    // 监听侧边栏的变化
    this.sidebarElm = document.getElementsByClassName('sidebar-container')[0]
    this.sidebarElm && this.sidebarElm.addEventListener('transitionend', this.sidebarResizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    if (this.autoResize) {
      window.removeEventListener('resize', this.__resizeHandler)
    }

    this.sidebarElm && this.sidebarElm.removeEventListener('transitionend', this.sidebarResizeHandler)

    this.chart.dispose()
    this.chart = null
  },
  methods: {
    getData(timeType) {
      let nowTime = this.$moment(new Date()).format('YYYY-MM-DD HH:mm:ss')
      let param = { timeType: timeType, nowDate: nowTime }
      findResAndConNum(param).then(res => {
        this.taskData = res
        this.initChart()
      })
    },
    sidebarResizeHandler(e) {
      if (e.propertyName === 'width') {
        this.__resizeHandler()
      }
    },
    setOptions() {
      var myChart = this.$echarts.init(document.getElementById('macarons'))
      myChart.setOption({
        tooltip: {
          trigger: 'axis',
          backgroundColor: 'rgba(0,0,0,0.4)',
          textStyle: {
            color: '#FFFFFF'
          },
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        legend: {
          data: this.taskData.legend,
          orient: 'horizontal',
          height: 200,
          x: 'center',//图例位置，设置right发现图例和文字位置反了，设置一个数值就好了
          y: 'top'//延Y轴居中
        },

        grid: {
          left: 30,
          right: 30,
          bottom: 20,
          top: 120,
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            data: this.taskData.times,
            boundaryGap: false,
            axisPointer: {
              type: 'shadow'
            },
            axisTick: {
              show: false
            },
            axisLine: {
              show: false
            },
            axisLabel: {
              show: true,
              textStyle: {
                color: '#131D33'
              }
            }
          }
        ],
        yAxis: [
          {
            type: 'value',
            name: '并发数',
            splitNumber: 6,
            axisLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            axisLabel: {
              show: true,
              textStyle: {
                color: '#C8CCDD'
              }
            },
            splitLine: {
              show: true,
              lineStyle: {
                width: 2,
                type: [2, 20],
                dashOffset: 5
              }
            },
            splitArea: { show: false }
          },
          {
            type: 'value',
            name: '资源数',
            splitNumber: 6,
            axisLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            axisLabel: {
              show: true,
              textStyle: {
                color: '#C8CCDD'
              }
            },
            splitLine: {
              show: true,
              lineStyle: {
                width: 2,
                type: [2, 20],
                dashOffset: 5
              }
            },
            splitArea: { show: false }
          }
        ],
        dataZoom: [
          {
            show: true, // 是否展示
            xAxisIndex: [0],  // 控制第一个x轴
            height: 15,
            left: 100,
            right: 100,
            bottom: 0,
            start: 0,  // 数据窗口范围的起始百分比。范围是：0 ~ 100。表示 0% ~ 100%。
            end: 80,  // 数据窗口范围的结束百分比。范围是：0 ~ 100。
            handleSize: '10%',
            zoomLock: true //是否锁定选择区域（或叫做数据窗口）的大小。如果设置为 true
          },
          {
            type: 'inside',  // 内置型数据区域缩放组件，用户可以在坐标系上通过鼠标拖拽、鼠标滚轮、手指滑动（触屏上）来缩放或漫游坐标系
            show: true,
            disabled: false,  // 是否停止组件的功能
            height: 15,
            left: 100,
            right: 100,
            start: 0,
            end: 80
          }
        ],
        series: this.taskData.seriesList
      })
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions()
    }
  }
}
</script>
